package com.yq.service.impl;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.google.common.collect.Maps;
import com.yq.core.entity.SysCorUserRoleEntity;
import com.yq.core.entity.SysUserEntity;
import com.yq.core.service.SysCorUserRoleService;
import com.yq.core.service.SysUserService;
import com.yq.core.util.Page;
import com.yq.service.annotation.AzerothAutoService;
import com.yq.service.dao.BaseMapper;
import com.yq.service.dao.SysUserMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * 信  息: [yinquan] [2017-05-04] [1.0]
 * 描  述: 管理员 Service 实现
 */
@AzerothAutoService("sysUserService")
public class SysUserServiceImpl extends BaseServiceImpl<SysUserEntity, String> implements SysUserService {

    private static Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysCorUserRoleService sysCorUserRoleService;

    public BaseMapper<SysUserEntity, String> getBaseMapper() {
        return sysUserMapper;
    }

    /** 人员列表查询 **/
    public Page<Map<String, Object>> pageUserList(Map<String, Object> parameter, Page page) {
        PageBounds pageBounds = new PageBounds(page.getPageCurrent(), page.getPageSize(), true);
        List<Map<String, Object>> list = sysUserMapper.pageUserList(parameter, pageBounds);
        PageList<Map<String, Object>> pageList = (PageList) list;
        page.setRows(pageList);
        page.setTotalPageNum(pageList.getPaginator().getTotalPages());// 总页数
        page.setTotal(pageList.getPaginator().getTotalCount());// 总记录数
        return page;
    }

    /** 账户查询 **/
    public String findAccount() {
        Map<String, Object> map = sysUserMapper.findAccount();
        int baseNumbwe = 100000;
        if(map!=null && map.get("account")!=null) {
            baseNumbwe = Integer.parseInt(map.get("account").toString()) + 1;
        } else {
            baseNumbwe = baseNumbwe +1;
        }
        return baseNumbwe+"";
    }

    /** 获取人员角色关联 **/
    public List<Map<String, Object>> userRoleTree(Map<String, Object> parameter) {
        return sysUserMapper.userRoleTree(parameter);
    }

    /** 管理员授权 **/
    public void updateAccredit(Map<String, Object> parameter) {

        Map<String, Object> par = Maps.newHashMap();

        String roleUids = parameter.get("roleUids").toString();
        String userUid = parameter.get("userUid").toString();

        // 删除【管理员 - 角色】关联数据
        par.put("userUid", userUid);
        sysCorUserRoleService.deleteAccredit(par);

        String[] uids = roleUids.split(",");

        for(String u:uids) {
            // 更新【管理员 - 角色】关联表
            SysCorUserRoleEntity sysCorUserRoleEntity = new SysCorUserRoleEntity();
            sysCorUserRoleEntity.setUserUid(userUid);
            sysCorUserRoleEntity.setRoleUid(u);

            sysCorUserRoleService.insert(sysCorUserRoleEntity);

        }
    }
}